
import Element.elem

object Spiral {
  
  val space = elem(" ")
  
  val corner = elem("+")
  
  def spiral(nEdges: Int, direction: Int): Element = {
    if (nEdges == 1)
      elem("+")
    else {
      val sp = spiral(nEdges - 1, (direction + 3) % 4)
      def verticalBar = elem('|',sp.height, 1)
      def horizonalBar = elem('-', 1, sp.width)
      if (direction == 0)
        (corner beside horizonalBar) above (sp beside space)
      else if (direction == 1)
        (sp above space) beside (corner above verticalBar)
      else if (direction == 2)
        (space beside sp) above (horizonalBar beside corner)
      else 
        (verticalBar above corner) beside (space above sp)
    }
  }
  
  def main(args: Array[String]): Unit = {
    val nSide = args(0).toInt
    println(spiral(nSide, 0))
//    val elem1 = elem(Array("hello", "joe"))
//    val elem2 = elem(Array("lilei", "xiahao", "xiaowei"))
//    println(elem1 beside elem2)
//    
//    println("-----------")
//    
//    println(elem1 above elem2)
  }
}